/*-------------------------------------------------------------------------
 - Copyright (c) 2024-2025 [XD-AMCC TEAM]
 - [XD-AMCC] is licensed under Mulan PSL v2.
 - You can use this software according to the terms and conditions of the Mulan PSL v2.
 - You may obtain a copy of Mulan PSL v2 at:
 -          http://license.coscl.org.cn/MulanPSL2
 - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
 - OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 - TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 - See the Mulan PSL v2 for more details.
 ------------------------------------------------------------------------*/


/**
 * @brief  Recursive-Descent-Function of non-terminal jumpStatement.
 *
 * @author  WXQ#XDU
 * @date    2024.12
 */

#include "../_parser_inner.h"

#define THIS_NT_ID  jumpStatement

// jumpStatement               //------------------ LL(1) production 52..55
//    :  (  BREAK
//       |  CONTINUE
//       |  GOTO ID     // label ID is defined in labeledStatement
//       |  RETURN ( expression )?
//       )
//       ';'
//
t_tree_ptr RDF_jumpStatement (t_parser_ptr pThis) {
    int productionId = PRD_ID_UNKNOWN;
    t_tree_ptr root = tree_new_parent(THIS_NT, productionId);
    enter_nonterminal ( pThis, THIS_NT, root );
    CHECK_NULLTREE_ABORT ( root, root );
// -------  BEGIN of local action

    // eat the key-word firstly
    EnumTokenKind keyword = LOOK_AHEAD(pThis);
    t_tree_ptr   subTree = parser_match(pThis, keyword);
    CHECK_NULLTREE_ABORT (subTree, root);
    tree_add_child(root, subTree);

    if (TK_KW_BREAK == keyword)
        productionId = 52;
    else if (TK_KW_CONTINUE == keyword)
        productionId = 53;
    else if (TK_KW_GOTO == keyword) {
        productionId = 54;
        subTree = parser_match(pThis, TK_ID);  // consume 'ID' -- lable name
        CHECK_NULLTREE_ABORT (subTree, root);
        tree_add_child(root, subTree);
    }
    else if (TK_KW_RETURN == keyword){
        productionId = 55;
        if ( TK_SEMI != LOOK_AHEAD(pThis) ) {  // optional return-expr
            subTree = RDF_expression(pThis);
            CHECK_NULLTREE_ABORT (subTree, root);
            tree_add_child(root, subTree);
        }
    }

    subTree = parser_match(pThis, TK_SEMI); // consume ';'
    CHECK_NULLTREE_ABORT (subTree, root);
    tree_add_child(root, subTree);

// -------  END of local action
    if (PRD_ID_UNKNOWN == productionId) {
        ERROR_REPORT_BUG("Forgot to correct the value of "
                         "productionId in RDF_" NT_TEXT "()");
    }
    tree_reset_production( root, productionId );

LABEL_EXIT_RDF:
    exit_nonterminal  ( pThis, THIS_NT, root );
    return root;
}

